package net.ipinglun.activitys;

import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import net.ipinglun.R;
import net.ipinglun.models.NewsDetailBean;
import net.ipinglun.presenters.NewsDetailPresenter;

import java.util.ArrayList;
import java.util.List;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;
import pl.droidsonroids.gif.GifImageView;

public class NewsDetailActivity extends SwipeBackActivity {

    private String mNewsId;
    private NewsDetailPresenter mPresenter;
    private ListView mListView;
    private RelativeLayout mHeaderView;
    private RelativeLayout mFooterView;
    private WebView mWebView;
    private GifImageView mLoadingView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }


        Bundle bundle = this.getIntent().getExtras();
        mNewsId = bundle.getString("id");

        mListView = (ListView)findViewById(R.id.list_view);
        mLoadingView = (GifImageView)findViewById(R.id.loading);

        mHeaderView = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.news_detail_header, null);
        mWebView = (WebView) mHeaderView.findViewById(R.id.web_view);
        mListView.addHeaderView(mHeaderView);

        mFooterView = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.waterfall_footer, null);
        mListView.addFooterView(mFooterView);

        mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, getData()));

        mPresenter = new NewsDetailPresenter(this);
        mPresenter.getNewsDetail(mNewsId);
    }


    public void onLoadNewsDetailStart() {

    }

    public void onLoadNewsDetailSuccess(NewsDetailBean newsDetail) {
        //System.out.println("newsDetail=" + newsDetail.title);
        ((RelativeLayout)mLoadingView.getParent()).removeView(mLoadingView);
        mListView.setVisibility(View.VISIBLE);

        mWebView.loadDataWithBaseURL("", newsDetail.content, "text/html", "utf-8", "");
    }

    public void onLoadNewsDetailFinish() {

    }

    public void onLoadNewsDetailFailure() {

    }

    private List<String> getData(){
        List<String> data = new ArrayList<String>();
        data.add("测试数据1");
        data.add("测试数据2");
        data.add("测试数据3");
        data.add("测试数据4");
        return data;
    }


    public void clickFinish(View v) {
        super.finish();
    }

}
